{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d3ddb0e",
   "metadata": {},
   "source": [
    "# 基础信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "711127f5",
   "metadata": {},
   "source": [
    "## Ndarray 对象、数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b1821ea9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.20.1\n",
      "[[1 2 3]]\n",
      "<class 'numpy.int8'> int64 int8\n",
      "2\n",
      "[[1]\n",
      " [2]\n",
      " [3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "print(np.__version__)\n",
    "arr = np.array([1,2,3], dtype = 'i4', copy = True, order = None, subok = False, ndmin = 2)\n",
    "print(arr)\n",
    "# 内置对象\n",
    "print(np.int8, np.dtype(np.int0), np.dtype('i1'))\n",
    "# 维度\n",
    "print(arr.ndim)\n",
    "# 转置\n",
    "print(arr.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa0f5010",
   "metadata": {},
   "source": [
    "## 创建数组、数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9df8fc4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[(b'', b'') (b'', b'') (b'', b'')]\n",
      "  [(b'', b'') (b'', b'') (b'', b'')]\n",
      "  [(b'', b'') (b'', b'') (b'', b'')]]\n",
      "\n",
      " [[(b'', b'') (b'', b'') (b'', b'')]\n",
      "  [(b'', b'') (b'', b'') (b'', b'')]\n",
      "  [(b'', b'') (b'', b'') (b'', b'')]]\n",
      "\n",
      " [[(b'', b'') (b'', b'') (b'', b'')]\n",
      "  [(b'', b'') (b'', b'') (b'', b'')]\n",
      "  [(b'', b'') (b'', b'') (b'', b'')]]]\n",
      "[[[0 0]\n",
      "  [0 0]]\n",
      "\n",
      " [[0 0]\n",
      "  [0 0]]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n",
      "[1. 2. 3.]\n",
      "[[[1 2 3 4]]]\n",
      "[[[1 2 3 4]]]\n",
      "[(b'abc', 11) (b'cdf', 22)]\n",
      "属性 1 24 (24,) 8 int64\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]\n",
      "  [ 9 10 11]]\n",
      "\n",
      " [[12 13 14]\n",
      "  [15 16 17]\n",
      "  [18 19 20]\n",
      "  [21 22 23]]]\n",
      "3\n",
      "140262655524464\n",
      "140262655524560\n",
      "[ 0  5 10 15]\n",
      "[ 0  5 10 15]\n",
      "[0.         0.08715574 0.17364818 0.25881905]\n",
      "0 15 15\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "print(np.empty([3,3,3],dtype=[('t1','S'),('t2','S')]))\n",
    "print(np.zeros((2,2,2),dtype='i1'))\n",
    "print(np.ones([2,3]))\n",
    "\n",
    "it=iter([1,2,3])\n",
    " \n",
    "# 使用迭代器创建 ndarray \n",
    "x=np.fromiter(it, dtype=float)\n",
    "print(x)\n",
    "\n",
    "# print(np.arange(5))\n",
    "# print(np.arange(1,10,2))\n",
    "# print(np.linspace(1,20,20))\n",
    "# print(np.logspace(1.0,2.0,num=10))\n",
    "print(np.arange(10))\n",
    "print(np.arange(10)[2:4])\n",
    "print(np.arange(10)[2:8:2])\n",
    "\n",
    "# 最小维度\n",
    "data_a = np.array([1,2,3,4],dtype=int,ndmin=3)\n",
    "print(data_a)\n",
    "print(np.asarray(data_a))\n",
    "# 定义类型\n",
    "type=np.dtype([('name','S20'),('age','i1')])\n",
    "print(np.array([('abc',11),('cdf',22)],dtype=type))\n",
    "\n",
    "data=np.arange(24)\n",
    "print(\"属性\",data.ndim, data.size, data.shape, data.itemsize, data.dtype)\n",
    "# 定义维度\n",
    "data=data.reshape(2,4,3)\n",
    "# 维度输出\n",
    "print(data)\n",
    "print(data.ndim)\n",
    "\n",
    "a = np.arange(2,10,2)\n",
    "c = a.view()\n",
    "print(id(a))\n",
    "print(id(c))\n",
    "\n",
    "# 最值\n",
    "a=np.arange(0,20,5)\n",
    "print(a.flatten())\n",
    "print(a)\n",
    "print(np.sin(a*np.pi/180))\n",
    "print(np.amin(a),np.amax(a),np.ptp(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72392134",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(8)\n",
    "# print(a.reshape(4,2))\n",
    "# print(a.reshape(4,2).reshape(2,4))\n",
    "# for x in a.reshape(4,2).flat:\n",
    "#     print(x,end=',')\n",
    "b = a.reshape(2,4)\n",
    "print(b)\n",
    "\n",
    "print(b)\n",
    "b.ravel()\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0519a01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "print(np.arange(20).reshape(2,10))\n",
    "print(np.arange(20).reshape(2,10).T)\n",
    "for x in np.nditer(np.arange(20).reshape(2,10).T):\n",
    "    print(x,end=',')\n",
    "print('\\n')\n",
    "for x in np.nditer(np.arange(20).reshape(2,10).T,order='C'):\n",
    "    print(x,end=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b566b3bc",
   "metadata": {},
   "source": [
    "# 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c70a3db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8de9ef4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# print(np.empty([3,2],dtype=int))\n",
    "# print(np.zeros(5,dtype=int))\n",
    "# print(np.zeros((2,3),dtype=[('x','i4'),('y','i4')]))\n",
    "# print(np.ones([2,3]))\n",
    "print(np.asarray(np.ones([2,3])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cef0eed6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a=np.arange(24)\n",
    "# print(a)\n",
    "# print(a.ndim)\n",
    "# print(a.reshape(2,4,3))\n",
    "# print(a.reshape(2,4,3).ndim)\n",
    "# print(a.reshape(2,4,3).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a31ddb6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "print(np.array([1,2,3],dtype=complex))\n",
    "print(np.dtype(np.int32))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc2877fd",
   "metadata": {},
   "source": [
    "# IO处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1083e70e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "## 单个\n",
    "data=np.arange(20)\n",
    "np.save('data.npy',data)\n",
    "print(np.load('data.npy'))\n",
    "\n",
    "## 多个\n",
    "np.savez('allData.npz',np.arange(10).reshape(2,5),np.arange(10),np.arange(4))\n",
    "allData = np.load('allData.npz')\n",
    "print(allData.files)\n",
    "for i in allData.files:\n",
    "    print(i,allData[i])\n",
    "\n",
    "## txt\n",
    "np.savetxt('sava.txt',np.array([1,2,3]))\n",
    "print(np.loadtxt('sava.txt'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "527px",
    "left": "21px",
    "top": "191px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
